package jomm.utils.db;

import java.io.BufferedReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * User: ngraca
 * Date: 3/10/11
 * Time: 10:18 AM
 */
public class DBUtils {

    public static void executeSQLFile(BufferedReader br, Connection con) throws IOException, SQLException {

        StringBuffer strBuf = new StringBuffer();
        String line = null;
        while ((line = br.readLine()) != null) {
            if (line.length() > 0 && !line.startsWith("--")) {
                strBuf.append(line).append("\n");
            }
        }

        String[] toks = strBuf.toString().split(";\n|;\r\n|;\r");
        for (String tok : toks) {
            if (tok.trim().length() > 0) {
                PreparedStatement ps = con.prepareStatement(tok);
                ps.execute();
                ps.close();
            }
        }
    }
}
